
package net.vinote.smart.platform.service.dw;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import javax.servlet.http.HttpServletRequest;

import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.log.RunLogger;
import net.vinote.smart.platform.service.dw.html.HtmlTag;
import net.vinote.smart.platform.service.util.ShowStyle;


/**
 * 数据窗体的列表页面
 */
public class DataWindowList extends DataWindow
{

	private boolean edit;

	public DataWindowList(HttpServletRequest request)
	{
		super(request);
		setShowStyle(ShowStyle.LIST);
	}

	@Override
	protected String getDWHtml() throws SQLException
	{
		if (getDwCatalog() == null)
		{
			RunLogger.getLogger().log(Level.WARNING,
					"显示模板:" + getDoNo() + "未定义!");
			return "<span>显示模板:" + getDoNo() + "未定义!</span>";
		}
		// 加载列表中要展示的字段
		// Map<String, HtmlTag> tagMap = getHtmlTagMap();
		DBManager bm = DBManagerFactory.getInstance("dataobject_library");
		List<BizObject> fieldList = bm.createQuery(new String[]
		{
				"FIELDNAME", "COLFIELD"
		}, "DoNo='" + getDoNo() + "' and isinuse='1' order by sortno")
				.getResultList();
		StringBuffer sb = new StringBuffer(
				"<table cellpadding='0' cellspacing='0' border='0' class='dwList' id='"
						+ getDoNo() + "'>");
		sb.append("<thead><tr><td>&nbsp;</td>");
		int fieldSize = fieldList.size();
		String[] fieldArray = new String[fieldSize];
		for (int i = 0; i < fieldSize; i++)
		{
			sb.append("<td><pre>")
					.append(fieldList.get(i).getAttribute("FIELDNAME"))
					.append("</pre></td>");
			fieldArray[i] = fieldList.get(i).getAttribute("COLFIELD");
		}
		sb.append("</tr></thead><tbody>");
		// 加载List表中展示的数据
		List<BizObject> queryList = getDataList();
		int i = 0;
		if (edit)
		{
			for (BizObject queryBo : queryList)
			{
				Map<String, HtmlTag> tagMap = getHtmlTagMap(queryBo);
				sb.append("<tr><td>").append(++i).append("</td>");
				for (String field : fieldArray)
				{
					HtmlTag tag = tagMap.get(field).clone();
					tag.setId(tag.getId() + i);
					tag.setName(tag.getName() + "@" + i);
					tag.setValue(queryBo.getAttribute(field));
					tag.setClass(tag.getColField());
					sb.append("<td>").append(tag.getHtml()).append("</td>");
				}
				sb.append("<td><input type='hidden' name='O.isinsert@")
						.append(i).append("' value='false' /></td></tr>");
			}
			sb.append("<tr><td>").append(++i).append("</td>");
			// 以下为处于新增状态
			setInsert(true);
			BizObject bo = new BizObject();
			Map<String, HtmlTag> tagMap = getHtmlTagMap(bo);
			for (String field : fieldArray)
			{
				HtmlTag tag = tagMap.get(field).clone();
				tag.setId(tag.getId() + i);
				tag.setName(tag.getName() + "@" + i);
				tag.setClass(tag.getColField());
				tag.setValue(bo.getAttribute(field));
				sb.append("<td>").append(tag.getHtml()).append("</td>");
			}
			sb.append("<td><input type='hidden' name='O.isinsert@").append(i)
					.append("' value='true' /></td></tr>");
		}
		else
		{
			for (BizObject queryBo : queryList)
			{
				sb.append("<tr><td>").append(++i).append("</td>");
				for (String field : fieldArray)
				{
					sb.append("<td><span id='")
							.append(field + i)
							.append("' name='")
							.append(field)
							.append("'>")
							.append(StringFunction.nullToBlank(queryBo
									.getAttribute(field)))
							.append("</span><input type='hidden' value='")
							.append(StringFunction.nullToBlank(queryBo
									.getAttribute(field))).append("' class='")
							.append(field).append("'/></td>");
				}
				sb.append("</tr>");
			}
		}
		sb.append("</tbody></table>");
		return sb.toString();
	}

	public List<HtmlTag> getDataGirdHead()
	{
		return null;
	}

	public boolean isEdit()
	{
		return edit;
	}

	public void setEdit(boolean edit)
	{
		this.edit = edit;
	}
}
